import 'package:example/arg/demo3arg.dart';
import 'package:example/router/example_routes.dart';
import 'package:example/util/widget_util.dart';
import 'package:flutter/material.dart';
import 'package:riki_router/riki_router.dart';

@RikiRoute(
  name: '/demo/2',
  routeName: '有基础参数路由',
  pageRouteType: PageRouteType.cupertino,
)
class Demo2Page extends StatelessWidget {
  const Demo2Page({this.text, this.num, this.isTrue}) : super();

  @RikiArgument()
  final String? text;
  @RikiArgument('数字')
  final int? num;
  @RikiArgument('是否真实')
  final bool? isTrue;

  factory Demo2Page.init1(String? text) => Demo2Page(text: text);

  factory Demo2Page.init2(int? num) => Demo2Page(num: num);

  factory Demo2Page.init3(bool? isTrue) => Demo2Page(isTrue: isTrue);

  @override
  Widget build(BuildContext context) {
    RikiPage? route = RikiRouterDelegate.of(context).find(this);
    return Scaffold(
      appBar: buildAppBar('DEMO 2'),
      body: ListView(
        children: [
          Container(
            alignment: Alignment.center,
            height: 44,
            child: Text(route?.routeName ?? ''),
          ),
          buildListTile('text:$text', () {}),
          buildListTile('num:$num', () {}),
          buildListTile('isTrue:$isTrue', () {}),
          Visibility(
            child: buildListTile('返回首页', () {
              RikiRouterDelegate.of(context).popUntil((page) => page.name == RikiRoutes.root);
            }),
            visible: isTrue ?? false,
          ),
          Visibility(
            child: buildListTile('返回首页并跳转DEMO3', () {
              RikiRouterDelegate.of(context).pushNamedAndRemoveUntil(RikiRoutes.demo3.name, (page) => page.name == RikiRoutes.root,
                  arguments: RikiRoutes.demo3.d(
                      arg: Demo3Arg(
                    '2',
                    1,
                    true,
                    ['1', '2', '3'],
                    {'1': 1, '2': '123123213', '3': true},
                  )));
            }),
            visible: isTrue ?? false,
          ),
          Visibility(
            child: buildListTile('返回上级并进入新页面', () {
              RikiRouterDelegate.of(context).popAndPushNamed(
                RikiRoutes.demo3.name,
                arguments: RikiRoutes.demo3.d(
                    arg: Demo3Arg(
                  '2',
                  1,
                  true,
                  ['1', '2', '3'],
                  {'1': 1, '2': '123123213', '3': true},
                )),
                transitionMilliseconds: 1000,
                reverseTransitionMilliseconds: 2000,
              );
            }),
            visible: isTrue ?? false,
          ),
        ],
      ),
    );
  }
}
